<template>
  <div class="main-body">
    <!-- 头部标题 -->
    <div class="main">
      <div class="header">
        <span class="title orange">企业工作台</span>
        <span class="symbol">></span>
        <span class="title orange">采购报价看板管理</span>
        <span class="symbol">></span>
        <span class="title gray">发布物料</span>
      </div>
      <!-- form表单 -->
      <div class="form-tab">
        <!-- Tab切换栏 -->
        <div class="tab-bar">
          <div class="btn-left" :class="{ btnFocus: btnOrder == 1 }" @click="onTab(1)">发布物料</div>
          <div class="btn-right" :class="{ btnFocus: btnOrder == 2 }" @click="onTab(2)">批量添加</div>
          <span class="line"></span>
        </div>
        <!-- Tab 切换 第一页 发布物料 -->
        <div v-if="pageOne">
          <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" size="mini">
            <!-- 第一区块  应用类 -->
            <div class="area-color margin-bot">
              <el-form-item label="应用类" prop="use">
                <el-input class="input-width" v-model="ruleForm.use" placeholder="请输入物料用途，使用场景"></el-input>
              </el-form-item>
              <el-form-item label="物料类" prop="region">
                <!-- 第一个不可用的选择框 -->
                <el-select placeholder="原材料" disabled class="input-width">
                  <el-option label="区域一" value="shanghai"></el-option>
                </el-select>
                <!-- 三级联动选择框 -->
                <el-select v-model="sheng" @change="choseProvince" placeholder="请选择大类" class="item-space">
                  <el-option v-for="item in province" :key="item.id" :label="item.value" :value="item.id">
                  </el-option>
                </el-select>
                <el-select v-model="shi" @change="choseCity" placeholder="请选择子类" class="item-space">
                  <el-option v-for="item in shi1" :key="item.id" :label="item.value" :value="item.id">
                  </el-option>
                </el-select>
                <el-select v-model="qu" @change="choseBlock" placeholder="请选择小类" class="item-space">
                  <el-option v-for="item in qu1" :key="item.id" :label="item.value" :value="item.id">
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="物料名称" prop="name">
                <el-input class="input-width" v-model="ruleForm.name" placeholder="请输入物料名称"></el-input>
              </el-form-item>
              <el-form-item label="截止日期" required>
                <el-col :span="11">
                  <el-form-item prop="date1">
                    <el-date-picker type="date" placeholder="请选择截止日期" v-model="ruleForm.date1" style="width: 220px;"></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
            </div>
            <!-- 第二区块 需求量 -->
            <div class="area-color margin-bot" v-show="need">
              <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm" size="mini">
                <!-- 三条验证内容 -->
                <el-form-item label="需求量" prop="ton">
                  <el-input class="input-width" v-model="ruleForm.ton" placeholder="请填写月度总用量"></el-input>
                </el-form-item>
                <el-form-item label="交货地点" prop="place">
                  <el-input class="input-width" v-model="ruleForm.place" placeholder="请输入交货地点"></el-input>
                </el-form-item>
                <el-form-item label="交易时间" required>
                  <el-col :span="11">
                    <el-form-item prop="time">
                      <el-date-picker type="date" placeholder="请选择交易时间" v-model="ruleForm.time" style="width: 220px;"></el-date-picker>
                    </el-form-item>
                  </el-col>
                </el-form-item>
                <el-form-item label="质量等级">
                  <el-input class="input-width" placeholder="请输入质量等级"></el-input>
                </el-form-item>
                <el-form-item label="包装方式">
                  <el-input class="input-width" placeholder="请输入包装方式"></el-input>
                </el-form-item>
                <el-form-item label="技术标准">
                  <el-input class="input-width" placeholder="请输入技术标准"></el-input>
                </el-form-item>
              </el-form>
            </div>
            <!-- 第三区块  结算方式 -->
            <div class="area-color">
              <el-form-item label="结算方式" prop="resource">
                <el-radio-group v-model="ruleForm.resource">
                  <el-radio label="现金电汇" class="radio-space" @click.native="openMoney(1)"></el-radio>
                  <br>
                  <el-radio label="承兑汇票" class="radio-space" @click.native="openMoney(2)"></el-radio>
                  <br>
                  <el-radio label="预付定金" class="radio-space" @click.native="openMoney(3)"></el-radio>
                  <!-- 预付定金控制的贷款内容 -->
                  <div class="money">
                    <el-input-number v-model="num" controls-position="right" :min="0" :max="100" :disabled="money"></el-input-number>
                  </div>
                  <span class="input-text">%;剩余货款</span>
                  <div class="money">
                    <el-input v-model="pay" placeholder="请输入内容" :disabled="money"></el-input>
                  </div>
                  <span class="input-text">支付</span>
                  <br>
                  <el-radio label="账期" class="radio-space" @click.native="openMoney(4)"></el-radio>
                  <!-- 期账控制的天数内容 -->
                  <div class="day">
                    <el-input v-model="days" placeholder="请输入内容" :disabled="day"></el-input>
                  </div>
                  <span class="input-text">天</span>
                  <div class="money">
                    <el-select v-model="value" placeholder="请选择" :disabled="day">
                      <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value">
                      </el-option>
                    </el-select>
                  </div>
                </el-radio-group>
              </el-form-item>
            </div>
            <el-form-item>
              <el-button class="btn-ok" type="primary" @click="submitForm">发布</el-button>
            </el-form-item>
          </el-form>
        </div>
        <!-- tab 切换 第二页 -->
        <div v-else>
          <el-form ref="ruleForm" label-width="100px" class="demo-ruleForm" size="mini">
            <!-- 第一区块  应用类 -->
            <div class="area-color">
              <el-form-item label="批量添加">
                <el-input class="input-width" placeholder="请输入物料用途，使用场景"></el-input>
              </el-form-item>
              <el-form-item label="批量添加">
                <el-select placeholder="原材料" disabled class="input-width">
                  <el-option label="区域一" value="shanghai"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="批量添加">
                <el-input class="input-width" placeholder="请输入物料名称"></el-input>
              </el-form-item>
              <el-form-item label="截止日期">
                <el-col :span="11">
                  <el-form-item>
                    <el-date-picker type="date" placeholder="请选择截止日期" style="width: 220px;"></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
            </div>
            <el-form-item>
              <el-button class="btn-ok" type="primary">发布</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import axios from 'axios'
export default {
  name: 'Main',
  data () {
    return {
      // 第二块 显示
      need: false,
      // Tab 切换
      pageOne: true,
      // Tab按钮样式改变
      btnOrder: 1,
      num: 1,
      day: true,
      days: '',
      pay: '',
      value: '',
      money: true,
      // 三级联动
      province: '',
      sheng: '',
      sheng1: '',
      shi: '',
      shi1: [],
      shi2: '',
      // 小类名 matter
      qu: '',
      qu1: [],
      city: '',
      block: '',
      msg: '',
      activeName: 'first',
      // 选择支付方式
      options: [{
        value: '选项1',
        label: '银行卡'
      }, {
        value: '选项2',
        label: '支付宝'
      }, {
        value: '选项3',
        label: '微信'
      }],
      // form 表单
      ruleForm: {
        // 应用类 use
        use: '',
        // 物料名称 name
        name: '',
        date1: '',
        type: [],
        resource: '',
        ton: '',
        place: '',
        time: ''
      },
      // 验证内容
      rules: {
        use: [
          { required: true, message: '请输入应用类', trigger: 'blur' }
        ],
        name: [
          { required: true, message: '请输入应用类', trigger: 'blur' },
          { min: 1, max: 10, message: '长度在 3 到 20 个字符', trigger: 'blur' }
        ],
        date1: [
          { type: 'date', required: true, message: '请选择截止日期', trigger: 'change' }
        ],
        resource: [
          { required: true, message: '请选择结算方式', trigger: 'change' }
        ],
        ton: [
          { required: true, message: '请输入需求量', trigger: 'blur' }
        ],
        place: [
          { required: true, message: '请输入交易地点', trigger: 'blur' }
        ],
        time: [
          { type: 'date', required: true, message: '请输入交货时间', trigger: 'change' }
        ]
      }
    }
  },
  methods: {
    // 弹出警告框
    openCenter () {
      this.$message({
        message: '请先填写表格内容！',
        center: true,
        type: 'warning'
      })
    },
    // form 表单 提交按钮
    submitForm () {
      if (this.ruleForm.use && this.sheng1 && this.ruleForm.name) {
        var newDate = {
          use: '',
          matter: '',
          name: ''
        }
        newDate.use = this.ruleForm.use
        newDate.matter = this.sheng1
        newDate.name = this.ruleForm.name
        // vuex 传值 更新数据
        this.$store.dispatch('getData', newDate)
        // 路径跳转
        this.$router.push('/Manage')
      } else {
        this.openCenter()
        return false
      }
    },
    // Tab 切换
    onTab (num) {
      this.btnOrder = num
      if (num === 1) {
        this.pageOne = true
      } else {
        this.pageOne = false
      }
    },
    // radio 预付定金和账期控制开关
    openMoney (num) {
      if (num < 3) {
        this.money = true
        this.day = true
      } else if (num === 3) {
        this.money = false
        this.day = true
      } else {
        this.day = false
        this.money = true
      }
    },
    // 三级联动 加载数据
    getCityData: function () {
      var that = this
      // 获取数据
      axios.get('/static/json/material.json').then(function (response) {
        if (response.status === 200) {
          var data = response.data
          that.province = []
          that.city = []
          that.block = []
          // 分类数据
          for (var item in data) {
            if (item.match(/0000$/)) {
              that.province.push({id: item, value: data[item], children: []})
            } else if (item.match(/00$/)) {
              that.city.push({id: item, value: data[item], children: []})
            } else {
              that.block.push({id: item, value: data[item]})
            }
          }
          // 第二级子类 分类
          for (var index in that.province) {
            for (var index1 in that.city) {
              if (that.province[index].id.slice(0, 2) === that.city[index1].id.slice(0, 2)) {
                that.province[index].children.push(that.city[index1])
              }
            }
          }
          // 第三级小类 分类
          for (var item1 in that.city) {
            for (var item2 in that.block) {
              if (that.block[item2].id.slice(0, 4) === that.city[item1].id.slice(0, 4)) {
                that.city[item1].children.push(that.block[item2])
              }
            }
          }
        } else {
          console.log(response.status)
        }
      }).catch(function (error) { console.log(typeof +error) })
    },
    // 第一级大类 change
    choseProvince: function (e) {
      for (var index2 in this.province) {
        if (e === this.province[index2].id) {
          this.shi1 = this.province[index2].children
          this.shi = this.province[index2].children[0].value
          this.qu1 = this.province[index2].children[0].children
          this.qu = this.province[index2].children[0].children[0].value
          this.E = this.qu1[0].id
          this.sheng1 = this.province[index2].value
          this.msg = this.sheng1 + '/' + this.shi + '/' + this.qu
        }
      }
      // 控制第二区块的出现
      if (this.sheng1 === '钢材' || this.sheng1 === '化工') {
        this.need = true
      } else {
        this.need = false
      }
    },
    // 第二级子类 change
    choseCity: function (e) {
      console.log(e, 'City')
      for (var index3 in this.city) {
        if (e === this.city[index3].id) {
          console.log(this.city[index3].value)
          this.qu1 = this.city[index3].children
          this.qu = this.city[index3].children[0].value
          this.E = this.qu1[0].id
          this.shi2 = this.city[index3].value
          this.msg = this.sheng1 + '/' + this.shi2 + '/' + this.qu
        }
      }
    },
    // 第三极小类 change
    choseBlock: function (e) {
      this.E = e
      for (var index4 in this.block) {
        if (e === this.block[index4].id) {
          console.log(this.block[index4].value)
          this.E = this.block[0].id
          this.msg = this.sheng1 + '/' + this.shi2 + '/' + this.block[index4].value
        }
      }
    },
    setData () {
      var newData = this.$store.state.newData
      this.ruleForm.use = newData.use
      this.ruleForm.name = newData.name
    }
  },
  // 挂载函数
  mounted () {
    // 获取三级联动数据
    this.getCityData()
    // 复制物料跳转 初始化数据
    this.setData()
  }
}
</script>

<style lang="less" scoped>
  @import "~styles/total.less";
  // 颜色
  .orange {
    color: @orange;
    font-weight: bold;
  }
  .gray {
    color: gray;
  }
  // 修改elemen-ui默认样式
  .margin-bot {
    margin-bottom: 20px;
  }
  .btn-ok {
    margin-left: -100px;
    width: 100px;
    height: 30px;
    font-size: 15px;
    background-color: @orange;
    border: 1px solid @orange;
  }
  .el-tabs__item {
    margin-left: 20px;
  }
  .el-input__inner {
    width: 100px;
  }
  .item-space {
    margin-left: 40px;
  }
  .radio-space {
    margin-top: 6px;
    margin-bottom: 8px;
  }
  .input-width {
    width: 220px;
  }
  .main-body {
    width: 100%;
    overflow: auto;
    .main {
      width: 1350px;
      margin: 0 auto;
      .header {
        width: 100%;
        height: 75px;
        line-height: 75px;
        border-bottom: 2px solid #e9e7e7;
        .title {
          font-size: 20px;
        }
        .symbol {
          font-size: 18px;
          margin: 0 15px;
        }
      }
      .form-tab {
        margin-top: 20px;
        .tab-bar {
          position: relative;
          width: 100%;
          height: 40px;
          margin-bottom: 30px;
          div {
            position: absolute;
            border-radius: 6px 6px 0 0;
            width: 80px;
            height: 35px;
            line-height: 40px;
            text-align: center;
            background-color: #f3f3f3;
            font-size: 14px;
            border:3px solid #e5e5e5;
            bottom: -4px;
          }
          .btn-left {
            left: 40px;
          }
          .btn-right {
            left: 140px;
          }
          .line {
            display: block;
            position: absolute;
            bottom: -3px;
            width: 100%;
            height: 3px;
            background-color: @orange;
          }
          .btnFocus {
            color: @orange;
            background-color: #fff;
            border:3px solid @orange;
            border-bottom: 3px solid #fff;
            z-index: 1;
          }
        }
        .area-color {
          padding-top: 20px;
          padding-bottom: 10px;
          background-color: #f3f3f3;
          .money {
            display: inline-block;
            width: 130px;
            font-size: 16px;
          }
          .input-text {
            margin: 0px 20px 0px 10px;
            font-size: 15px;
          }
          .day {
            display: inline-block;
            width: 130px;
            font-size: 16px;
            margin-left: 28px;
          }
        }
      }
    }
  }
</style>
